package android.snake;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.view.Display;
import android.view.KeyEvent;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;

public class Game extends Activity {
	// The game board.
	private Board board;
	
	// The snake.
	private Reptile snake;
	
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        
        board = new Board(15,15);
        snake = new Reptile(board.getX() / 2, board.getY() / 2);
        board.getBoard()[board.getX() / 2][board.getY() / 2] = Case.snake;
        setContentView(new GameView(this));
    }

	public void refreshGame ()
	{
			
	}
	
	class GameView extends ViewGroup {
        public GameView(Context context) {
            super(context);
            
            ImageView leftArrow = new ImageView(context);
            leftArrow.setAdjustViewBounds(true);
            leftArrow.setMaxHeight(20);
            leftArrow.setMaxWidth(20);
            leftArrow.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
            leftArrow.setImageDrawable(new BitmapDrawable(BitmapFactory.decodeResource(getResources(), R.drawable.left_arrow)));
            leftArrow.setScaleType(ScaleType.CENTER);
            
            ImageView rightArrow = new ImageView(context);
            rightArrow.setAdjustViewBounds(true);
            rightArrow.setMaxHeight(20);
            rightArrow.setMaxWidth(20);
            rightArrow.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
            rightArrow.setImageDrawable(new BitmapDrawable(BitmapFactory.decodeResource(getResources(), R.drawable.right_arrow)));
            rightArrow.setScaleType(ScaleType.CENTER);
            
            ImageView upArrow = new ImageView(context);
            upArrow.setAdjustViewBounds(true);
            upArrow.setMaxHeight(20);
            upArrow.setMaxWidth(20);
            upArrow.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
            upArrow.setImageDrawable(new BitmapDrawable(BitmapFactory.decodeResource(getResources(), R.drawable.up_arrow)));
            upArrow.setScaleType(ScaleType.CENTER);
            
            ImageView downArrow = new ImageView(context);
            downArrow.setAdjustViewBounds(true);
            downArrow.setMaxHeight(20);
            downArrow.setMaxWidth(20);
            downArrow.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
            downArrow.setImageDrawable(new BitmapDrawable(BitmapFactory.decodeResource(getResources(), R.drawable.down_arrow)));
            downArrow.setScaleType(ScaleType.CENTER);
            
            this.addView(leftArrow, 0);
            this.addView(rightArrow, 1);
            this.addView(upArrow, 2);
            this.addView(downArrow, 3);
        }

        // Propagate the layout to all children views contained.
		protected void onLayout(boolean changed, int l, int t, int r,
				int b) {
        	for (int i = 0; i < getChildCount(); i++)
        		getChildAt(i).layout(0, 0, 0, 0);
        	Display display = getWindowManager().getDefaultDisplay(); 
        	int width = display.getWidth();
        	int height = display.getHeight();
        	
        	// Left arrow.
        	getChildAt(0).setPadding(2 * width / 5, (int) (height * 1.5), 0, 0);
        	// Right arrow.
        	getChildAt(1).setPadding(8 * width / 5, (int) (height * 1.5), 0, 0);
        	// Up arrow.
        	getChildAt(2).setPadding(width, (int) (height * 1.3), 0, 0);
        	// Down arrow.
        	getChildAt(3).setPadding(width, (int) (height * 1.70), 0, 0);
		}
        
        // Return to main menu if the back key is pressed while in-game
        @Override
        public boolean onKeyDown(int keyCode, KeyEvent event)
        {
        	if (event.getKeyCode() == KeyEvent.KEYCODE_BACK)
        		finish ();
        	return(true);
        }
        
        // Draw the board and dispatch draw to all children contained in the group.
        @Override
        public void dispatchDraw(Canvas canvas) {
        	canvas.drawColor(Color.BLACK);
        	
        	Display display = getWindowManager().getDefaultDisplay(); 
        	int width = display.getWidth();
        	int height = display.getHeight();
        	
        	int offsetHeight = (height - (board.getX() * 12)) / 3;
        	int offsetWidth = (width - (board.getY() * 12)) / 2;
        	int i = 0;
        	while (i < board.getX())
        	{
        		int j = 0;
        		while (j < board.getY())
        		{
        			Bitmap bmp;
        			if (board.getBoard()[i][j] == Case.wall)
        				bmp = BitmapFactory.decodeResource(getResources(), R.drawable.b_wall);
        			else if (board.getBoard()[i][j] == Case.empty)
        				bmp = BitmapFactory.decodeResource(getResources(), R.drawable.b_empty);
        			else
        				bmp = BitmapFactory.decodeResource(getResources(), R.drawable.b_snake);
        			canvas.drawBitmap(bmp, 12 * j + offsetWidth, 12 * i + offsetHeight, null);	
        			j++;
        		}
        		i++;
        	}
        	
        	for (int k = 0; k < getChildCount(); k++)
        		getChildAt(k).draw(canvas);
        	
        	// Tell the system to draw the view again.
        	//invalidate();
        }
	}
}